package com.eastjet.equipment.controller;


import com.eastjet.equipment.service.DeviceRulesService;
import com.eastjet.equipment.util.CodeRules;
import com.eastjet.maintenance.domain.DeviceRules;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/rules")
public class RulesController {

    @Autowired
    private DeviceRulesService deviceRulesService;
    @Autowired
    private CodeRules codeRules;


    @PostMapping("/test")
    @Qualifier
    public String test(@RequestParam("") Map<String, String> TableName) {
        // 检查输入的表名称是否为空
        if (TableName.get("TableName") == null || TableName.isEmpty()) {
            //返回错误信息
            return "TableName is null or empty";
        } else {
            System.out.println("tableName: " + TableName);
            System.out.println("tableName.get(TableName): " + TableName.get("TableName"));
            List<DeviceRules> list = deviceRulesService.getDeviceRulesByTableName(TableName.get("TableName"));
            if (list.size() == 0) {
                //返回错误信息
                return "DeviceRules Table is not have the TableName";
            } else {
                System.out.println("1564654654654564654564654564564");
                // 调用代码规则
                return codeRules.codeRules(list);
            }
        }
    }


    @RequestMapping("/deviceRulesList")
    public Page<DeviceRules> getDeviceRulesList(@RequestParam(defaultValue = "1") int page,
                                                @RequestParam(defaultValue = "100") int size,
                                                @RequestParam(defaultValue = "") String condition) {
        System.out.println("condition:" + condition);
        return deviceRulesService.getDeviceRules(page, size, condition);
    }

    @RequestMapping("/update")
    public String updateDeviceRules(@RequestBody  Map<String, Object> map) {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceRules deviceRules = new DeviceRules();
        deviceRules.setRulesId(Integer.parseInt(map.get("rulesId").toString()));
        deviceRules.setRulesFromname(map.get("rulesFromname").toString());
        if (map.get("rulesTimeType").toString().equals("yyyy")){
            deviceRules.setRulesTimeType(0);
        }else if (map.get("rulesTimeType").toString().equals("yyyyMM")){
            deviceRules.setRulesTimeType(1);
        }else if (map.get("rulesTimeType").toString().equals("yyyyMMdd")){
            deviceRules.setRulesTimeType(2);
        }else if (map.get("rulesTimeType").toString().equals("yyyyMMddHHmm")){
            deviceRules.setRulesTimeType(3);
        }else if (map.get("rulesTimeType").toString().equals("yyyyMMddHHmmss")){
            deviceRules.setRulesTimeType(4);
        }else {
            deviceRules.setRulesNum(5);
        }
        if (map.get("rulesNum").toString().equals("001")){
            deviceRules.setRulesNum(0);
        }else if (map.get("rulesNum").toString().equals("0001")){
            deviceRules.setRulesNum(1);
        }else if (map.get("rulesNum").toString().equals("00001")){
            deviceRules.setRulesNum(2);
        }
        return deviceRulesService.updateDeviceRules(deviceRules);
    }
}
